package com.glenet.salary.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.glenet.salary.entities.HrEmployeeJob;
import com.glenet.salary.util.SessionFactoryUtil;

public class HrEmployeeJobDao extends HrDaoSuport {
	public boolean save(HrEmployeeJob job){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.insert("HrEmployeeJob.save", job) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public int batchSave(List<HrEmployeeJob> list){
		int effect = 0;
		try {
			SqlSession session = SessionFactoryUtil.getSessionFactory().openSession(ExecutorType.BATCH);
			for (HrEmployeeJob job : list) {
				effect = +session.insert("HrEmployeeJob.save", job);
			}
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(effect);
		return effect;
	}
	
	public boolean update(HrEmployeeJob job){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.update("HrEmployeeJob.update", job) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public HrEmployeeJob get(Long eid){
		HrEmployeeJob job = new HrEmployeeJob();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			job = (HrEmployeeJob)session.selectOne("HrEmployeeJob.get", eid);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return job;
	}
	
	@SuppressWarnings("unchecked")
	public List<HrEmployeeJob> list(int offset, int limit){
		List<HrEmployeeJob> list = new ArrayList<HrEmployeeJob>();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			list = (List<HrEmployeeJob>)session.selectList("HrEmployeeJob.list", null, new RowBounds(offset, limit));
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
